package cn.tedu.reflect;

import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

//测试 暴力反射
public class Test3_violentReflect {
    public static void main(String[] args) throws Exception {
//        method();
//        method2();
        method3();
    }

    public static void method3() throws Exception{
        Class<Person> clazz = Person.class;
        Method m = clazz.getDeclaredMethod("show",null);
        Test test = m.getDeclaredAnnotation(Test.class);
        Object obj = clazz.newInstance();
        if (test != null){
            m.invoke(obj);
        }
    }

    public static void method2() throws Exception {
        Class<Person> clazz = Person.class;
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getName());
            System.out.println(field.getType().getName());
        }
        Field score = clazz.getDeclaredField("score");
        System.out.println(score.getName());
        System.out.println(score.getType().getName());
        Object obj = clazz.newInstance();
        score.setAccessible(true);
        score.set(obj, 6.6);
        System.out.println(score.get(obj));

    }

    public static void method() throws Exception{
        Class<Person> clazz = Person.class;
        Method[] ms = clazz.getDeclaredMethods();
        for (Method m : ms) {
            System.out.println(m.getName());
            Class[] cs = m.getParameterTypes();
            System.out.println(Arrays.toString(cs));
        }

        //暴力反射 某个方法
        Method game = clazz.getDeclaredMethod("game", String.class);
        game.setAccessible(true);
        Object obj = clazz.newInstance();
        game.invoke(obj,"蛇皮香蕉");
    }
}
